<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//购物车控制器
class ShoppingcarController extends Controller{
	public function index(){
	    View::make();
	}
	//购物车数据处理
	public function ajaxCar(){
	    //如果是异步请求
	    if(IS_AJAX){
	    	//获得gid
	    	$gid=Q('post.gid',0,'intval');
	    	//获得gaid
	    	$gaid=explode(',', substr(Q('post.gaid',array()),0, strlen(Q('post.gaid',array()))-1));
//			p($gaid);exit;
	    	//获得购买数量
	    	$num=Q('post.num',0,'intval');
	    	//通过gaid获得goods表里的所有数据
	    	$goodsModel=new \Common\Model\Goods;
			$goodsData=$goodsModel->where("gid={$gid}")->first();
//			p($goodsData);die;
	    	//关联商品属性表类型属性表
	    	foreach ($gaid as $k => $v) {
	    		$gaid[$k]=Db::table('goods_attr')->join('typeattr','typeattr_taid','=','taid')->where("gaid=$v")->first();	
	    	}
//			p($gaid);exit;
	    	//计算总价格
	    	$price=array();
			//获得商品表里的售价
//			$price[]=$goodsData['shopprice'];
			//获得gaid里面的价格
			foreach ($gaid as $k => $v) {
				$price[]=$goodsData['shopprice']+$v['added'];
			}
//			p($price);exit;
			//查询货品列表
			$strgaid=substr(Q('post.gaid',array()),0,strlen(Q('post.gaid',array()))-1);
//			p($strgaid);exit;
//			$glid=Db::table('goods_list')->where("goods_gid={$gid} AND combine='{$strgaid}'")->pluck('glid');
//			p($glid);exit;
	    	//用框架购物车
	    	$data = array();
			foreach ($gaid as $k => $v) {
				$data['id']=$goodsData['gid'];
				$data['name']=$goodsData['gname'];
				$data['num']=$num;
				$data['price']=$price;
				$data['pic']=$goodsData['pic'];
				$data['options']=array(
					'color'=>'red'
				);
			}
			
			
//			p($data);
//			
			Cart::add($data);
			
	    	//返回数据
	    	$bbdata= array(
				'name'=>$goodsData['gname'],
				'total'=>Cart::getTotalPrice(),
				'num'=>cart::getTotalNums()
			);
			p($bbdata);die;
			echo json_encode($bbdata);
			exit;

	    }
	}
}
 ?>